<template>
  <div>
    <el-dialog :visible.sync="dialogFormVisible" top="10vh">
      <div class="content">
        <el-form ref="subform1" :model="form" :rules="rules">
          <!-- 标题-->
          <el-form-item label="标题：" :label-width="formLabelWidth" prop="title">
            <el-input v-model="form.title" class="form" />
          </el-form-item>
          <!-- 排序-->
          <el-form-item label="排序：" :label-width="formLabelWidth" prop="sortWidget">
            <el-input-number
              v-model="form.sortWidget"
              :disabled="isDisabled"
              :min="1"
              style="width:150px; margin-right:16px; margin-bottom:10px;"
            />
          </el-form-item>
          <!-- 分类-->
          <el-form-item
            label="分类："
            :label-width="formLabelWidth"
            prop="cateType"
            :hidden="cateHidden"
          >
            <el-select
              v-model="form.cateType"
              placeholder="请选择分类"
              class="form"
              @change="changeCate"
            >
              <el-option
                v-for="item in cateData"
                :key="item.id"
                :label="item.categoryName"
                :value="item.id"
              />
            </el-select>
          </el-form-item>

          <!--          &lt;!&ndash; 封面图 &ndash;&gt;-->
          <!--          <el-form-item-->
          <!--            label="封面图"-->
          <!--            :label-width="formLabelWidth"-->
          <!--            prop="coverImg"-->
          <!--            :hidden="coverHidden"-->
          <!--          >-->

          <!--            <el-upload-->
          <!--              action="https://mpingweb.yuepinyuemei.com/api/upload"-->
          <!--              :data="{-->
          <!--                dirPath: 'authentication',-->
          <!--                upload_status: true }"-->
          <!--              :multiple="false"-->
          <!--              :limit="1"-->
          <!--              list-type="picture-card"-->
          <!--              :on-remove="handleRemove"-->
          <!--              :on-success="upload_main"-->
          <!--            >-->
          <!--              <i class="el-icon-plus" />-->
          <!--            </el-upload>-->
          <!--            <el-dialog>-->
          <!--              <img width="100%" :src="common.img_url+img_main" alt="">-->
          <!--            </el-dialog>-->
          <!--            <img width="20%" :src="common.img_url+form.coverImg" alt="">-->
          <!--          </el-form-item>-->
          <el-form-item label="图片尺寸：" :label-width="formLabelWidth" :hidden="banHidden">
            <el-input v-model="imageSize" :disabled="true" class="form" />
          </el-form-item>
          <!-- 轮播图/展示图 -->
          <el-form-item
            :label="imgLaber"
            :label-width="formLabelWidth"
            prop="bannerList"
            :hidden="banHidden"
          >
            <el-upload
              action="https://mpingweb.yuepinyuemei.com/api/upload"
              :data="{
                dirPath: 'authentication',
                upload_status: true }"
              :multiple="true"
              :limit="imgNum"
              :file-list="img_list"
              :show-file-list="false"
              list-type="picture-card"
              :on-success="upload_arr"
              :before-upload="before_main"
              :on-remove="del_arr"
            >
              <i class="el-icon-plus icon" />
            </el-upload>
            <ul class="img-list">
              <li v-for="(item, index) in img_list" :key="index" class="img-item">
                <img v-if="item.url" :src="item.url" alt class="img" />
                <div class="del-btn" @click.stop="del_arr(item)">
                  <i class="el-icon-delete" />
                </div>
              </li>
            </ul>
          </el-form-item>
          <el-form-item label="内容详情：" :label-width="formLabelWidth" prop="des">
            <tinymce id="addTalent" v-model="form.des" :min-height="750" :width="760" />
          </el-form-item>
        </el-form>
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button plain @click="closeMask">取 消</el-button>
        <el-button v-if="!isDisabled&&infos&&!infos.id" type="primary" plain @click="submitInfo">确 定</el-button>
        <el-button v-if="!isDisabled&&infos&&infos.id" type="primary" plain @click="resetInfo">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import tinymce from "@/components/Tinymce";
import { addArticle, updateArticle } from "../../api/article";
import { getCateDownList } from "../../api/category";
export default {
  components: {
    tinymce,
  },
  props: {
    isHidden: {
      type: Boolean,
      default: false,
    },
    isType: {
      type: Number,
      default: 0,
    },
    detailInfo: {
      id: "",
    },
    isDisabled: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      imgNum: 1, //图片最多上传数量
      imgLaber: "", //图片名称
      imageSize: "1:1尺寸",
      formLabelWidth: "120px",
      dialogFormVisible: this.isHidden,
      img_main: "", // 图片
      img_list: [], // 展示图
      img_arr: [], //   项目展示图
      infos: this.detailInfo, // 传过来的信息
      form: {
        title: "", // 标题
        coverImg: "", // 封面图
        bannerList: "", // 轮播图
        des: "", // 内容详情
        sortWidget: 0, // 排序
        cateType: null, // 分类
      },
      cateData: [], // 分类列表
      // coverHidden: true, // 主图组件是否隐藏，默认隐藏
      cateHidden: true, // 分类组件是否隐藏，默认隐藏
      banHidden: true, // 展示图组件是否隐藏，默认隐藏
      rules: {
        title: [{ required: true, message: "请输入标题", trigger: "blur" }],
        des: [
          {
            required: true,
            message: "请输入内容详情",
            trigger: "blur",
          },
        ],
        cateType: [
          {
            required: true,
            message: "请选择分类",
            trigger: "blur",
          },
        ],
        bannerList: [
          {
            required: true,
            message: "请上传展示图",
            trigger: "blur",
          },
        ],
        sortWidget: [
          {
            required: true,
            message: "请输入排序",
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    dialogFormVisible() {
      this.$emit("checkMask", this.dialogFormVisible);
    },
    // 展示图
    img_arr(val) {
      const arr = [];
      if (Array.isArray(val)) {
        val.forEach((item, index) => {
          arr.push({
            name: index,
            url: this.common.img_url + item,
          });
        });
        this.img_list = arr;
        this.$forceUpdate();
      }
    },
  },
  mounted() {
    // 文章类型判断
    if (this.isType === 1) {
      this.imgLaber = "展示图：";
      // this.coverHidden = true
      this.banHidden = false;
      this.cateHidden = false;
      this.imgNum = 4;
    }
    if (this.isType === 2) {
      this.imgLaber = "主图：";
      this.banHidden = false;
      this.imgNum = 1;
    }

    // 分类下拉选择
    getCateDownList({
      queryType: 0,
      showTalentShare: 1,
    })
      .then((res) => {
        this.cateData = res.data.data.list;
      })
      .catch((res) => {
        this.$message.error("网络连接错误，请稍后再试");
      });

    // 说明是查看或者编辑 而不是添加
    if (this.infos && this.infos.id) {
      this.form.title = this.infos.title;
      this.form.sortWidget = this.infos.sortWidget;
      this.form.des = this.infos.des;
      this.form.coverImg = this.infos.coverImg;
      this.img_main = this.infos.coverImg;
      this.form.cateType = this.infos.cateId;
      this.img_arr = this.infos.bannerList || [];
    }
  },
  methods: {
    // 分类选择
    changeCate() {},
    // 添加
    submitInfo() {
      if (this.checkInfo()) {
        addArticle({
          title: this.form.title, // 标题
          // coverImg: this.img_main, // 封面图
          bannerList: this.img_arr, // 轮播图
          des: this.form.des, // 内容详情
          cateId: this.form.cateType - 0,
          articleType: this.isType,
          userType: 2,
          sortWidget: this.form.sortWidget, // 排序
        })
          .then((res) => {
            if (res.data.code === 7001) {
              this.$message.error(res.data.msg);
            } else {
              this.$message({
                showClose: true,
                message: "添加" + res.data.msg,
                type: "success",
              });
              this.$emit("refresh", {});
            }
            this.closeMask();
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          });
      } else {
        // 弹出错误框
        this.$message({
          showClose: true,
          message: "请填写必要参数",
          type: "error",
        });
      }
    },

    // 修改
    resetInfo() {
      if (this.checkInfo()) {
        updateArticle({
          id: this.infos.id,
          title: this.form.title,
          // coverImg: this.img_main, // 封面图
          bannerList: this.img_arr, // 轮播图
          sortWidget: this.form.sortWidget,
          des: this.form.des, // 内容详情
          cateId: this.form.cateType,
          articleType: this.isType,
          userType: 2,
        })
          .then((res) => {
            if (res.data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
              });
              this.$emit("refresh", {});
            } else {
              this.$message.error(res.data.msg);
            }
            this.closeMask();
          })
          .catch((res) => {
            this.$message({
              showClose: true,
              message: res.data.msg,
              type: "error",
            });
          });
      } else {
        // 弹出错误框
        this.$message({
          showClose: true,
          message: "请填写必要参数",
          type: "error",
        });
      }
    },

    // 图片移除
    handleRemove() {
      this.img_main = "";
    },
    // 图片上传
    upload_main(res) {
      this.img_main = res.data;
    },

    // 上传展示图之前钩子
    before_main() {
      this.loading = true;
    },

    // 上传展示图
    upload_arr(res) {
      this.loading = false;
      const arr = [...this.img_arr];
      arr.push(res.data);
      this.img_arr = arr;
    },
    // 删除展示图
    del_arr(val) {
      const del_url = val.url.split(this.common.img_url)[1];
      this.img_arr.forEach((item, index) => {
        if (del_url === item) {
          this.img_arr.splice(index, 1);
        }
      });
    },

    closeMask() {
      this.$refs["subform1"].resetFields();
      this.dialogFormVisible = false;
      this.$emit("checkMask", this.dialogFormVisible);
    },

    // 检查参数
    checkInfo() {
      // if (!this.img_main) {
      //   this.img_main = this.form.coverImage
      // }
      if (this.isType !== 3 && !this.img_arr) {
        this.img_arr = this.form.bannerList;
      }
      if (this.isType === 1 && this.form.cateType === null) {
        return false;
      }
      return !(this.form.title === "" || this.form.des === "");
    },
  },
};
</script>
<style  lang="scss" scoped>
.content {
  padding-right: 1%;
  height: 470px;
  max-height: 480px;
  overflow: auto;
  box-sizing: border-box;
}
.form {
  width: 400px;
  margin-right: 16px;
  margin-bottom: 10px;
}
.img-list {
  display: inline-block;
  vertical-align: middle;
  .img-item {
    width: 150px;
    height: 150px;
    border: 1px solid #ccc;
    display: inline-block;
    vertical-align: middle;
    overflow: hidden;
    margin-right: 10px;
    position: relative;
    .img {
      width: 100%;
    }
    .del-btn {
      width: 100%;
      height: 100%;
      background-color: rgba(0, 0, 0, 0.3);
      color: #fff;
      line-height: 150px;
      text-align: center;
      font-size: 40px;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
      cursor: pointer;
      display: none;
    }
    &:hover {
      .del-btn {
        display: block;
      }
    }
  }
}
</style>
